﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace VirtualDisk
{
    class Parser
    {
        //解析参数个数无限制
        public  const  int NOLIMIT = -1;
        //去掉带参数命令中‘/’与字母或数字之间的空格
        public static MyString EaraseSpace(MyString str)
        {
            if (MyString.IsNullOrEmpty(str))
            {
                return MyString.Empty;
            }
            int index = str.IndexOf('/');
            if (index != -1)
            {
                return str.Substring(0, index+1) + EaraseSpace(str.Substring(index + 1).Trim());
            }
            else
                return str;
        }


        //将strToParse解析为命令和numofArgu个参数，并返回参数
        //如果为NOLIMIT则参数个数无限制
        public static MyString[] parseArgu(MyString strToParse, int numofArgu)
        {
            MyString strtemp = EaraseSpace(strToParse).Trim();
            if (strtemp.IndexOf(' ')!=-1)
            {
                strtemp = strtemp.Substring(strtemp.IndexOf(' ') + 1);
                
            }
            else
            {
                strtemp = MyString.Empty ;
            }

            MyString[] temp;
            if (numofArgu!=NOLIMIT)
            {
                temp = strtemp.Trim().Split(new char[] { ' ' }, numofArgu, StringSplitOptions.RemoveEmptyEntries);
            } 
            else
            {
               temp = strtemp.Trim().Split(new char[] { ' ' }, StringSplitOptions.RemoveEmptyEntries);
            }
            return temp;
            
        }

       
        //strToParse解析出命令名
        public static MyString parseCommandName(MyString strToParse)
        {
            MyString[] temp = strToParse.Trim().Split(new char[] { ' ' });
            return temp[0];
        }

       
    }
}
